from selenium.webdriver.common.by import By

from common.utils import chromeDriver


class ProcessOfPurchase:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://8.155.1.153/"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)

    def Purchase(self):
        #购买流程测试，对数码区商品进行测试
        for i in range(1, 9):
            #拿到商品的售价,原价,商品名称  比较首页和商品详情页售价，原价，商品名称是否相同
            selector_now = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > strong"
            element_now = self.driver.find_element(By.CSS_SELECTOR, selector_now)
            price_now_display = element_now.text    #首页显示的售价

            selector_prev = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > span.original-price.am-text-truncate"
            element_prev = self.driver.find_element(By.CSS_SELECTOR, selector_prev)
            price_prev_display = "-1"   #商品可能没有打折
            if len(element_prev) > 0:
                element = element_prev[0]
                price_prev_display = element.text

            #获取商品名称的文本
            selector_text = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({1}) > div > div > p.am-text-truncate-2-md.goods-title > a"
            element_text = self.driver.find_element(By.CSS_SELECTOR, selector_text)
            text_display = element_text.text
            #点击商品名称跳转到商品详情页
            self.driver.find_element(By.CSS_SELECTOR, selector_text).click()
            curwindow = self.driver.current_window_handle   #获取当前窗口句柄
            allwindow = self.driver.window_handles  #获取所有窗口句柄
            for window in allwindow:
                if window != curwindow:
                    self.driver.switch_to.window(window)
                    chromeDriver.GetScreenShot()
                    break

            #拿到商品详情页的售价和原价
            element_now = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > div > div > div.tb-detail-price-content > div.items.goods-price.goods-sale-price > dd > b")
            price_now_detail = element_now.text
            element_prev = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > div > div > div.tb-detail-price-content > div.items.goods-price.goods-original-price > dd > span > b")
            price_prev_detail = "-1"
            if len(element_prev) > 0:
                element = element_prev[0]
                price_prev_display = element.text.replace(" ", "")

            #拿到商品详情页的商品名称文本
            element_text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body - content - container > div.body - content - formal - container > div.system - goods - detail > div.item - inform.am - container.am - radius.theme - data - edit - event > div.clearfix - right > div > div.tb - detail - hd.am - margin - bottom - xs > h1")
            text_detail = element_text.text

            #如果商品详情页价格会浮动，只取-前的最小值去比较
            if "-" in price_now_detail:
                price_now_detail = price_now_detail.split("-")[0]
            if "-" in price_prev_detail:
                price_prev_detail = price_prev_detail.split("-")[0]

            if price_now_display != price_now_detail:
                print(f"首页数码区第{i}张图商品售价和详情页售价不同，首页售价{price_now_display}, 详情页售价{price_now_detail}")
            if  price_prev_display != price_prev_detail:
                print(f"首页数码区第{i}张商品原价和详情页原价不同，首页原价{price_prev_display}, 详情页售价{price_prev_detail}")
            if text_display != text_detail:
                print(f"首页数码区商品名称和详情页名称不同, 首页名称{text_display}, 详情页名称{text_detail}")

            # 读取商品数量
            count = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.theme-options.am-flex.am-flex-row.am-flex-items-center > dd > div > input")
            price_count = count.get_attribute("value")
            price_count = float(price_count)

            #在详情页的购买功能
            #点击购买
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-primary.buy-submit.buy-event.login-event").click()

            #三种购买方式：快递，同城，自提
            #快递
            #确定实付款金额和商品显示售价一致
            text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > div > div.base-real-pay > span.price > strong.nav-total-price")
            text = text.replace(" ", "").replace("￥", "")
            price_now_display = price_now_detail.replace(" ", "").replace("￥", "")
            text = float(text)
            price_now_display = float(price_now_display)
            expected = price_now_display * price_count
            if abs(text - expected) > 0.01:
                print(f"支付价格和表明的售价不一致，支付价格未{text}，售价为：{expected}")

            #点击提交订单
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > form > div > button")





